package org.collection.newcollection;

import java.util.Map;

import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.TreeMultimap;
import com.google.common.primitives.Ints;

public class IterableTest {

	public static void main(String[] args) {
		System.out.println("Hello!");
		
		Iterable<Integer> concatenated = Iterables.concat(Ints.asList(1, 2, 3),
				Ints.asList(4, 5, 6));
		
		System.out.println(concatenated.toString());
		
		Integer lastAdded = Iterables.getLast(concatenated);
		System.out.println(lastAdded);
		
		ImmutableSet digits = ImmutableSet.of("zero", "one", "two", "three",
				"five", "six", "seven", "eight", "nine");
		Function<String, Integer> lengthFunction = new Function<String, Integer>() {
			public Integer apply(String string) {
				return string.length();
			}
		};
		
		ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction);
		System.out.println(digitsByLength.toString());
		
		// 反转map
		System.out.println(digitsByLength.inverse().toString());
		
		System.out.println("------------------------------");
		ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
		multimap.putAll("b", Ints.asList(2, 4, 6));
		multimap.putAll("a", Ints.asList(4, 2, 1));
		multimap.putAll("c", Ints.asList(2, 5, 3));
		System.out.println(multimap.toString());
		
		TreeMultimap<Integer, String> inverse = Multimaps.invertFrom(multimap, TreeMultimap.create());
		System.out.println(inverse.toString());
		
		System.out.println("--------------------");
		Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
		SetMultimap<String, Integer> multimap1 = Multimaps.forMap(map);
		System.out.println(multimap1);
		
		Multimap<Integer, String> inverse1 = Multimaps.invertFrom(multimap1, HashMultimap.create());
	    System.out.println(inverse1);
	}
}
